package com.demo2do.boyle.common.entity.aduser;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.alibaba.fastjson.annotation.JSONField;
import com.demo2do.boyle.common.entity.AuditStatus;
import com.demo2do.core.utils.SignatureUtils;

/**
 * 广告主信息表
 * 
 * @author mohanlan leo
 */
@Entity
@Table(name = "aduser")
public class Aduser {

	@Id
	@GeneratedValue
	private Integer id;
	
    @Column(name = "tel")
    private String mobile;
    
    /** 密码SEED 15位Unicode随即数字 */
    private String salt;
    
    /** password = MD5(seed + MD5(user.setPassrod) ) */
    private String password;
    
    @Embedded
    private Profile profile;
    
    @Embedded
    private Company company;
    
    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Fetch(FetchMode.SELECT)
    @PrimaryKeyJoinColumn
    @JSONField(serialize = false)
	private AduserBalance aduserBalance;
    
    @Enumerated(EnumType.ORDINAL)
    private AuditStatus status;
    
    /** 余额[冗余] */
    private int balance;
    
    private Date createTime;
    
    /** 最后登录时间 */ 
    @Column(name = "last_login")
    private Date lastLoginTime;
    
    /** 最后登录IP */
    private String lastIp;

    /** 是否为系统用户 */
    @Column(name = "is_system")
    private boolean system;
    
    /** 是否删除 */
    @Column(name = "is_deleted")
    private boolean disabled;


    /**
     * The default constructor
     */
	public Aduser() {
		
	}
	
	/**
	 * @param mobile
	 */
	public Aduser(String mobile) {
		this.mobile = mobile;
	}
	
	/**
	 * Initialize aduser with aduser level and aduser balance
	 * 
	 * @return the aduser
	 */
	public Aduser initialize(AduserLevel aduserLevel, AduserBalance aduserBalance) {
		
		this.aduserBalance = aduserBalance;
		
		this.profile.initialize(aduserLevel);
		
		// create default password from mobile last 6 digitals
		this.password = SignatureUtils.makeMD5Signature(this.mobile.substring(this.mobile.length() - 6));
		
		this.status = AuditStatus.AUDITING;
		
		this.createTime = new Date();
		this.disabled = false;
		this.system = false;
		
		return this;
	}
	
	/**
	 * 
	 * @param profile
	 * @return
	 */
	public Aduser editProfile(Profile profile) {
		this.profile = profile;
		return this;
	}
	
	/**
	 * 
	 * @param company
	 * @return
	 */
	public Aduser editCompany(Company company) {
		this.company = company;
		return this;
	}
	
	/**
	 * 
	 * @param balance
	 * @return
	 */
	public Aduser deposit(int balance) {
		this.balance += balance;
		return this;
	}
	
	/**
	 * 
	 * @param balance
	 * @return
	 */
	public Aduser refund(int balance) {
		this.balance += balance;
		return this;
	}
	
	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}

	/**
	 * @return the salt
	 */
	public String getSalt() {
		return salt;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @return the profile
	 */
	public Profile getProfile() {
		return profile;
	}

	/**
	 * @return the company
	 */
	public Company getCompany() {
		return company;
	}

	/**
	 * @return the aduserBlance
	 */
	public AduserBalance getAduserBalance() {
		return aduserBalance;
	}

	/**
	 * @return the status
	 */
	public AuditStatus getStatus() {
		return status;
	}

	/**
	 * @return the balance
	 */
	public int getBalance() {
		return balance;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @return the lastLoginTime
	 */
	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	/**
	 * @return the lastIp
	 */
	public String getLastIp() {
		return lastIp;
	}

	/**
	 * @return the system
	 */
	public boolean isSystem() {
		return system;
	}

	/**
	 * @return the disabled
	 */
	public boolean isDisabled() {
		return disabled;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @param mobile the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	/**
	 * @param salt the salt to set
	 */
	public void setSalt(String salt) {
		this.salt = salt;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @param profile the profile to set
	 */
	public void setProfile(Profile profile) {
		this.profile = profile;
	}

	/**
	 * @param company the company to set
	 */
	public void setCompany(Company company) {
		this.company = company;
	}

	/**
	 * @param aduserBlance the aduserBlance to set
	 */
	public void setAduserBalance(AduserBalance aduserBlance) {
		this.aduserBalance = aduserBlance;
	}

	/**
	 * @param status the status to set
	 */
	public void setStatus(AuditStatus status) {
		this.status = status;
	}

	/**
	 * @param balance the balance to set
	 */
	public void setBalance(int balance) {
		this.balance = balance;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @param lastLoginTime the lastLoginTime to set
	 */
	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	/**
	 * @param lastIp the lastIp to set
	 */
	public void setLastIp(String lastIp) {
		this.lastIp = lastIp;
	}

	/**
	 * @param system the system to set
	 */
	public void setSystem(boolean system) {
		this.system = system;
	}

	/**
	 * @param disabled the disabled to set
	 */
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}
	
}